const express = require("express")
const router = require("./router")
const {connectMysql} = require("./db")
const path = require("path")

const app = express()
connectMysql() // 连接mysql
const crosHandler = (req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*")
    res.setHeader("Access-Control-Allow-Headers", "*")
    res.setHeader("Access-Control-Allow-Methods", "*")
    next()
}
console.log("获取本地得绝对路径~",path.resolve(__dirname));
console.log("获取本地得绝对路径~",path.resolve(__dirname+"/public" ));
app.use(express.static(path.resolve(__dirname+ "/public" )))
app.use(crosHandler)
app.use(express.json())
app.use(express.urlencoded({extended:false}))
app.use("/api",router)
app.get("/*",(req,res)=>{
    res.sendFile(path.resolve(__dirname+ "/public/index.html"))
})

app.listen("9000", () => {
    console.log("服务启动成功，监听9000端口");
})